package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Biao;
import com.hopu.pojo.Dengji;
import com.hopu.pojo.Djsf;
import com.hopu.pojo.Emp;
import com.hopu.service.BiaoService;
import com.hopu.util.DateFormatGetTime;
import com.hopu.util.ResultUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@RequestMapping("/biao")
@RestController
public class BiaoController {
    @Resource
    private BiaoService biaoServiceImpl;

    @PostMapping("/findAllBiao")
    private ResultUtils findAllBiao(@RequestParam(value = "current",defaultValue = "1") Integer current){

        try {
            PageHelper.startPage(current,6);
            List<Biao> allBiao = biaoServiceImpl.findAllBiao();

            PageInfo pageInfo = new PageInfo(allBiao);
            return new ResultUtils(200,"查询页面成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtils(9999,"查询页面失败",null);
        }
    }

    @PostMapping("/addBiao")
    public ResultUtils addBiao(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "hid")int hid,
            @RequestParam(value = "dkd")Float dkd,
            @RequestParam(value = "skd")Float skd,
            @RequestParam(value = "mkd")Float mkd

    ){
        try {

            Emp empLog = (Emp) request.getSession().getAttribute("empLog");

            int num = biaoServiceImpl.addBiao(hid,dkd,skd,mkd,new Date().getTime(),empLog.getEid());

            if (num==0){
                return new ResultUtils(400,"ERROR",null);
            }
            return new ResultUtils(200,"SUCCESS",1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/findBiaoByBid")
    public ResultUtils findBiaoByBid(int bid){

        Biao biao = biaoServiceImpl.findBiaoByBid(bid);

        return new ResultUtils(200,"查询页面成功",biao);

    }
    @PostMapping("/uppBiao")
    public ResultUtils uppBiao(Biao biao, HttpSession session){

            // 获取session 当前登录账号信息
            Emp empLog = (Emp)session.getAttribute("empLog");
            biao.setEid(empLog.getEid());
            biao.setMtime(new Date().getTime());

            int biao1 = biaoServiceImpl.uppBiao(biao);
            if (biao1<0){
                return new ResultUtils(400,"ERROR",null);
            }
            return new ResultUtils(200,"SUCCESS",1);


    }
}
